<?php
class order extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->orderObj = D('company_order');
		$this->order_logObj = D('company_order_log');

		$this->areaObj = D('company_area');
		$this->cateObj = D('company_cate');
		$this->zoneObj = D('company_one');
		// $this->deviceObj = D('site_device');
		$this->adminObj = D('company_admin');

		// $this->config = $this->site_setting['baoxiu'];

		$this->getUser();
		// $this->smsObj = D('sms');

		$this->auth_list = $this->adminObj->get_auth_list($this->admin);
		// dump($this->auth_list);exit;
		$this->areas = $this->auth_list['areas'];
		$this->cates = $this->auth_list['cates'];
		$this->zones = $this->auth_list['zones'];
		// dump($this->auth_list);exit;
		
	}

	public function index(){
		
		$cond = "status=1 and order_status!=99 ".$this->cond_base;
		
		if($this->tab){
			$cond .=" and order_status={$this->tab}";
		}
		//权限
		switch($this->admin['level']){
			case '0'://管理员
				$cond .=" ";
				break;
			case '1'://报修员
				$cond .=" and admin_id={$this->admin['id']}";
				break;
			case '2'://报修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
			case '3'://维修师傅
				$cond .=" and shifu_id={$this->admin['id']}";
				break;
			case '4'://维修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
		}

		// 加入查询条件
		if($this->tab){
			switch($this->tab){
				case '8':
					$cond .=" and order_status=99";
					break;
				default:
					$cond .=" and order_status={$this->tab}";
			}
		}
		// dump($cond);exit;
		//搜索
		if($this->keyword){
			$cond.=" and (title like '%".$this->keyword."%') ";
		}

		//搜索
		if($this->keyword){
			$cond.=" and (title like '%".$this->keyword."%' or intro like '%".$this->keyword."%' and local like '%".$this->keyword."%') ";
		}
		
		$result = $this->orderObj->getList($cond);
        if($result){
			// dump($result);exit;
			$data['list'] = $result['list'];

			$this->renderSuccess('获取成功','',$data);
		}else{
			$this->renderError('没有数据');
		}

	}


	public function orderinfo()
	{
		// foreach($this->orderStatus as $key => $val){
		// 	$v['id'] = $key;
		// 	$v['name'] = $val;
		// 	$tabs[] = $v;
		// }

		$tabs =  ['待派单','已派单','已接单','维修中','已完成','已评价'];
		foreach($tabs as $val){
			$v['name'] = $val;
			$v['count'] = 0;
			$list[] = $v;
		}
		$data['tabs'] = $list;
		$data['areas'] = $this->areas;
		$data['cates'] = $this->cates;
		$data['zones'] = $this->zones;
		// dump($data);exit;
		$this->renderSuccess('获取成功','',$data);
	}


	public function add(){
		if($this->isAjax()){
			$formData = $this->formData('formData');

			$shifu_id = $this->adminObj->get_shifu_id($formData,$this->config);
			
			$order_status = 1;
			if($this->config['bx_moshi']>1){
				$order_status = 2;
				$up_time = date('Y-m-d H:i:s');
			}

			$device_id = arg('device_id') ?? 0;
			if($device_id){
				$device = $this->deviceObj->findById($formData['device_id']);
				// $data['zone_id'] = $device['zone_id'];
			}

			$photo = $formData['photo'];
			$photo = json_encode($photo);
			// dump($photo);exit;

			$arrBase = array(
				'company_id' => $this->company['id'],
				// 'company_id' => $this->company['id'],
				'admin_id' => $this->admin['id'],
				'shifu_id' => $shifu_id,
				'title' => $formData['title'],
				'area_id' => $formData['area_id'],
				'cate_id' => $formData['cate_id'],
				'zone_id' => $formData['zone_id'],
				// 'device_id' => $device_id,
				'local' => $formData['local'],
				'intro' => $formData['intro'],
				'photo' => $photo,
				'order_status' => $order_status,
				'add_time' => date('Y-m-d H:i:s'),
				'up_time' => $up_time,
			);

			
			$order_id = $this->orderObj->add($arrBase);
			if($order_id){
				//log
				$data['order_status'] = $order_status;
				$this->order_logObj->add_log($order_id,$data);

				//发送短信
				$this->orderObj->sendsms($order_id,$order_status,$this->config['baoxiu']);
			}
			$this->renderSuccess('提交成功','',$order_status);
		}

		//扫码
		//
		// $this->device_id = arg('device_id');
		// if($this->device_id){
		// 	$this->device = $this->deviceObj->get_detail($this->device_id);
		// }

		// $this->info = array('status'=>1);
		// $this->display($this->routeUrl."/set.html");
		
	}

	//详情
	public function detail(){
		$id = arg('id');
		$info = $this->orderObj->get_detail($id);
		// dump($info);exit;
		if($info){
			$this->info = $info;
			$this->admin = $this->adminObj->get_info($info['admin_id']);
			$this->shifu = $this->adminObj->get_info($info['shifu_id']);

			if($info['device_id']){
				$this->device = $this->deviceObj->get_detail($info['device_id']);
			}

			$this->renderSuccess('提交成功','',$info);
		}
	}

	//操作订单
	public function order_act(){
		if($this->isAjax()){
			$formData = $this->formData('formData');

			$photo = $formData['photo'];
			$photo = json_encode($photo);

			$order_status = $formData['order_status'];
			$order_id = $formData['info_id'];

			$data['order_id'] = $order_id;
			$data['order_status'] = $order_status;
			$data['photo'] = $photo;
			$data['shifu_id'] = $this->admin['id'];

			$result = $this->orderObj->order_act($order_id, $data);
			if($result){
				//log
				$this->order_logObj->add_log($order_id,$data);
				//发送短信给师傅
				// $this->smsObj->send_user_sms($order_id,$this->company_setting['sms'],'company');
				$this->orderObj->sendsms($order_id,$order_status,$this->config['baoxiu']);

				// $this->ajaxMsg('操作成功',1,'/'.$this->routeUrl.'/index?tab='.$data['order_status']);
			}
			

			// $act_arr = array(
			// 	'2'=>'重派订单',
			// 	'3'=>'确认订单',
			// 	'4'=>'订单维修中',
			// 	'5'=>'完成订单',
			// 	'6'=>'评价订单',
			// 	'7'=>'结算订单',
			// 	'99'=>'取消订单',
			// );
			// $this->act_name = $act_arr[$order_status];
			// $this->order_status = $order_status;

			// $info = $this->modelObj->get_detail($order_id);
			// if($info){
			// 	$this->info = $info;
			// 	// $this->area = $this->areaObj->get_info($info['area_id']);
			// 	$this->cate = $this->cateObj->get_info($info['cate_id']);
			// 	$this->zone = $this->zoneObj->get_info($info['zone_id']);
			// 	$this->admin = $this->userObj->get_info($info['admin_id']);
			// 	$this->shifu = $this->userObj->get_info($info['shifu_id']);
			// }
			$this->renderSuccess('提交成功','',$result);
		}
	}


	//评价
	public function pingjia(){
		$order_id = arg('id');
		if($this->fmdo == 'pingjia'){
			$data = $this->postData('data');

			$order_status = 6;
			$arrBase = array(
				'star_wx' =>arg('star_wx'),
				'star_sf' =>arg('star_sf'),
				'star_info' =>arg('star_info'),
				'order_status' =>$order_status,
				'up_time' =>date('Y-m-d H:i:s'),
				'status' =>1,
			);
			$result = $this->modelObj->edit($order_id, $arrBase);
			if($result){
				//log
				$data['order_status'] = $order_status;
				$this->order_logObj->add_log($order_id,$data);
				//发送短信给师傅
				// $this->smsObj->send_user_sms($order_id,$this->company_setting['sms'],'company');

				$this->ajaxMsg('评价成功',1,'/'.$this->routeUrl.'/index?tab='.$data['order_status']);
			}
		}

		$this->info = $this->modelObj->get_info($order_id);
	}

	//查看评价
	public function pingjia_look(){
		$id = arg('id');
		$info = $this->modelObj->get_info($id);
		
		if($info['star_wx']){
			for ($i=1; $i <= $info['star_wx']; $i++) { 
				$star_wx[$i] = 'checked';
			}
			for ($i=1; $i <= $info['star_sf']; $i++) { 
				$star_sf[$i] = 'checked';
			}
			$this->star_wx = $star_wx;
			$this->star_sf = $star_sf;
		}

		$this->info = $info;
	}


	//结算
	function jiesuan(){
		$id = arg('id');
		if($this->fmdo == 'jiesuan'){
			$order_status = 6;
			$arrBase = array(
				// 'yongjin'=>arg('yongjin'),
				// 'yj_money'=>arg('yj_money'),
				'order_status' =>$order_status,
				'up_time' =>date('Y-m-d H:i:s'),
				'status' =>1,
			);
			$this->modelObj->update(array('id'=>$id), $arrBase);
			
			$data = array(
				'msg'=>'佣金已结算',
				'intro'=>arg('intro'),
				'photo'=>joinStr(arg('photo')),
				'yongjin'=>arg('yongjin'),
				'yj_money'=>arg('yj_money'),
			);
			$this->order_logObj->add_log($order_id,$data);

			//发送短信
			D('sms')->send_web_sms($order_id);

			$this->ajaxMsg('信息结算成功',1);
		}

		$info = $this->modelObj->findById($id);
		// $info['yj_money'] = $info['jiage'] * 0.4;
		$this->info = $info;
	}

	

	public function ajax_child(){
		$data['type'] = arg('type');
		$data['id'] = arg('id');
		$data[$data['type'].'_ids'] = $this->user[$data['type'].'_ids'];

		$result = $this->modelObj->get_child($data);
		if($result){
			$this->list = $result;
			$this->type = $data['type'];
		}

	}

}